<?php

function mobile_service_account_upgrade_step1($parameters){
    global $user;

    $user_info = _get_user_info($user->uid);
    insertDefaultFarmer($user->uid);
    $record = nonghua_data_load('farmer', 'farmer_id', $user->uid);
    if($user_info['role'] ==='升级成农户'){
        return ['success'=>1,'roles'=>$user->roles,"status"=>$user_info['status'],'farmer'=>$record];
    }
    $role_result = user_role_load_by_name('升级成农户');
    $new_user = array(
        'signature_format' => 'filtered_html',
        'status' => 1,
        'language' => 'zh-hans',
        'timezone' => 'Asia/Shanghai',
        'roles' => array(
            $role_result->rid => '升级成农户',
        ),
    );
    $account = user_save($user, $new_user);
    drupal_static_reset("user_access");
    return ['success'=>1,'roles'=>$user->roles,"status"=>$record->status,'farmer'=>$record];
}
function mobile_service_account_upgrade_step2($parameters){
    global $user;
    $user_info = _get_user_info($user->uid);
    if($parameters['role']=='升级成农户'){
        module_load_include('inc', 'farmer', 'farmer.edit.page');
        $form_state['values'] = $parameters;
        $form_state['account'] = $user;
        $form_state['values']['user_type'] = 'farmer';
        $form_state['mobile_service']=true;
        drupal_form_submit('farmer_edit_form', $form_state,!empty($parameters['farmer_id'])?$parameters['farmer_id']:"");
        if ($errors = form_get_errors()) {
//            return services_error(implode(" ", $errors), 406, array('form_errors' => $errors));
            return ['success'=>0,'form_errors' => $errors];
        }
        else {
            return ['success'=>1];
        }
    }
}